package com.song.tools.songtest.service;

import com.song.tools.mvc.service.AggregationService;
import com.song.tools.mvc.validator.StrNotBlankValidator;
import com.song.tools.mvc.validator.ValidationResult;
import com.song.tools.mvc.validator.ValidatorChain;
import com.song.tools.songtest.beans.poi.DictionaryImportDto;
import com.song.tools.songtest.beans.request.DictionaryQueryRequest;
import com.song.tools.songtest.beans.request.DictionarySaveRequest;
import com.song.tools.songtest.beans.request.DictionaryUpdateRequest;
import com.song.tools.songtest.beans.response.DictionaryDeleteResponse;
import com.song.tools.songtest.beans.response.DictionaryQueryResponse;
import com.song.tools.songtest.beans.response.DictionarySaveResponse;
import com.song.tools.songtest.beans.response.DictionaryUpdateResponse;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * 字典服务
 *
 * @author song tools
 */
public interface DictionaryService extends AggregationService<Long, DictionaryQueryRequest, DictionaryQueryResponse,
        DictionarySaveRequest, DictionarySaveResponse, DictionaryUpdateRequest, DictionaryUpdateResponse, DictionaryDeleteResponse, HttpServletResponse> {

    @Override
    default ValidationResult validateOnSave(DictionarySaveRequest dictionarySaveRequest) {
        ValidatorChain validatorChain = new ValidatorChain();
        validatorChain.add(() -> new StrNotBlankValidator("code").validate(dictionarySaveRequest::getCode));
        validatorChain.add(() -> new StrNotBlankValidator("name").validate(dictionarySaveRequest::getName));
        return validatorChain.validate();
    }

    @Override
    default ValidationResult validateOnUpdate(DictionaryUpdateRequest dictionaryUpdateRequest) {
        ValidatorChain validatorChain = new ValidatorChain();
        validatorChain.add(() -> new StrNotBlankValidator("dictionaryId").validate(dictionaryUpdateRequest::getDictionaryId));
        return validatorChain.validate();
    }


    /**
     * 导入前的数据校验
     *
     * @param importDtos 导入dto
     */
    default ValidationResult validateOnImport(List<DictionaryImportDto> importDtos) {
        for (DictionaryImportDto importDto : importDtos) {
            ValidatorChain validatorChain = new ValidatorChain();
            validatorChain.add(() -> new StrNotBlankValidator("code").validate(importDto::getCode));
            validatorChain.add(() -> new StrNotBlankValidator("name").validate(importDto::getName));
            ValidationResult validationResult = validatorChain.validate();
            if (!validationResult.isValid()) {
                return validationResult;
            }
        }
        return ValidationResult.success();
    }
}
